/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-12-16 17:28:21
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2023-12-17 03:38:11
 * @FilePath: \koa-server\src\controller\blog.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

const { 
  getBlog,
  addBlog,
  getOneBlog,
  searchSelect,
  viewBlog, 
  likeBlog,
  search,
  getOne,
  insertItem,
  updateItem,
  deleteOne,
  searchLike
 } = require('../service/blog.service')
const { success,error } = require('../utils/response')
class blogControllrt {
  async getblog(ctx,next) {
    const params = ctx.request.body
    try{
      const res = await getBlog(params)
      if(res){
        let boj = {
          pageNo:params.pageNo,
          pageSize:params.pageSize,
          total:res.count
        }
        success(ctx,res.rows,null,200,boj)
      } else {
        error(ctx)
      }
    } catch(err)  {
      error(ctx,null)
    }
  }
  async getOne(ctx,next){
    const params = ctx.request.body
    try{
      const res = await getOneBlog(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      error(ctx,null)
    }
  }
  async updateView(ctx,next){
    const params = ctx.request.body
    try{
      const res = await viewBlog(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      console.log(err)
      error(ctx,null)
    }
  }
  async updateLike(ctx,next){
    const params = ctx.request.body
    try{
      const res = await likeBlog(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      error(ctx,null)
    }
  }
  async addblog(ctx,next){
    const params = ctx.request.body
    try{
      const res = await addBlog(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      error(ctx,null)
    }
  }
  async getSelectBlog(ctx,next){
    const params = ctx.request.body
    try{
      const res = await searchSelect(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      // console.log(err)
      error(ctx,null)
    }
  }
  //分页查询
  async getCunt(ctx,next){
    const params = ctx.request.body
    try{
      const res = await search(params)
      if(res){
        let boj = {
          pageNo:params.pageNo,
          pageSize:params.pageSize,
          total:res.count
        }
        success(ctx,res.rows,null,200,boj)
      } else {
        error(ctx)
      }
    } catch(err)  {
      console.log(err)
      error(ctx,null)
    }
  }
  //新增
  async addList(ctx,next){
    const params = ctx.request.body
    try{
      const res = await insertItem(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    }catch(err){
      console.log(err)
      error(ctx,null)
    }
  }
  //修改
  async editList(ctx,next){
    const params = ctx.request.body
    try{
      const res = await updateItem(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    }catch(err){
      console.log(err)
      error(ctx,null)
    }
  }
  //删除
  async deleteList(ctx,next){
    const params = ctx.request.body
    try{
      const res = deleteOne(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    }catch(err){
      console.log(err)
      error(ctx,null)
    }
  }
  //查询一条信息
  async getOneList(ctx,next){
    const params = ctx.request.body
    try{
      const res = await getOne(params)
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    }catch(err){
      console.log(err)
      error(ctx,null)
    }
  }
  //模糊查询
  async getBlogSearch(ctx,next){
    const params = ctx.request.body
    try{
      const res = await searchLike(params)
      if(res){
        let boj = {
          pageNo:params.pageNo,
          pageSize:params.pageSize,
          total:res.count
        }
        success(ctx,res.rows,null,200,boj)
      } else {
        error(ctx)
      }
    } catch(err)  {
      console.log(err)
      error(ctx,null)
    }
  }
}

module.exports = new blogControllrt()